package cn.ljq.controller.adm;

import cn.ljq.dao.ClzMapper;
import cn.ljq.entity.Clz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/adm/clz")
public class AdmClzController {

    @Autowired
    ClzMapper clzMapper;

    @RequestMapping("/show")
    public String show(Integer curpage,Model model){

        if (curpage == null)curpage=1;

        //curpage 当前页数

        System.out.println("当前页数：" + curpage);

        int total = clzMapper.getTotal();  //总行数

        System.out.println("总行数：" + total);

        int page = total/6;

        if (total % 6 != 0){   //总行数除以行数后 有余数的话
            page = page + 1;    //总页数 +1
        }

        System.out.println("总页数：" + page);

        model.addAttribute("curpage",curpage);  //给当前页数

        model.addAttribute("page",page);  //给总页数

        int n1 = (curpage - 1) * 6;  //从第几行开始

        int n2 = 6;  //输出几行

        model.addAttribute("clzs",clzMapper.findPage(n1,n2));

        return "adm/clz/page";
    }


    @GetMapping("/go2add")
    public String go2add(Model model){

        model.addAttribute("actmsg","添加班级");

        model.addAttribute("action","add");

        model.addAttribute("clzs",new Clz());

        return "forward:/adm/clz/show";

    }

    @PostMapping("/add")
    public String add(Clz clz,Model model){

        clzMapper.add(clz);  //添加新的班级

        return "forward:/adm/clz/show";

    }


    @GetMapping("/go2update")
    public String go2Update(String clzno ,Model model){

        model.addAttribute("actmsg","修改班级");

        model.addAttribute("action","update");

        System.out.println("clzno=" + clzno);  //html给的班级编号

        Clz clz = clzMapper.findOne(clzno);  //找到对应ID的班级

        model.addAttribute("clz",clz);  //把那个班级给所有数据到 html

        model.addAttribute("clzno",clz.getClzno());  //给单个数据
        model.addAttribute("clzname",clz.getClzname());

        return "forward:/adm/clz/show";

    }

    @PostMapping("/update")
    public String update(Clz clz,Model model){

        clzMapper.update(clz);  //直接修改

        return "forward:/adm/clz/show";

    }

    @GetMapping("/del")
    public String del(String clzno,Model model){

        model.addAttribute("clzs",new Clz());  //new 一个空对象给前端 防止空指针

        clzMapper.del(clzno);

        return "forward:/adm/clz/show";

    }

    @GetMapping("/prevpage")
    public String prevpage(Integer curpage,Model model){   //上一页操作

        if (curpage == null)curpage=1;  //如果当前页数是null，则定义为1  防止浏览器直接访问这个路径

        if (curpage>1)curpage--;  //如果当前页数不小于1 则可以继续上一页

        model.addAttribute("clzs",new Clz());  //给一个空值 避免空指针

        return "forward:/adm/clz/show?curpage=" + curpage;  //把页数给到前端
    }

    @GetMapping("/nextpage")
    public String nextpage(Integer curpage,Model model){   //下一页操作

        if (curpage == null)curpage=1;  //如果当前页数是null，则定义为1  防止浏览器直接访问这个路径

        Integer count = clzMapper.getTotal();  //获取总行数

        if (curpage == -1){  //如果当前页数是-1
            curpage = count/6;  //最后一页
        }

        if (curpage * 6< count)curpage++; //当前页数*行数小于总行数  则当前页数+1

        model.addAttribute("clzs",new Clz());  //给一个空值 避免空指针

        return "forward:/adm/clz/show?curpage=" + curpage;  //把页数给到前端
    }

}
